﻿# coding: utf-8

import ConfigParser
import os
import wx
import string
        
class GlobalSetting:
    def __init__(self):
        self._resourceDir = os.getcwd()
        self._modelDir = os.getcwd()
        self._meshDir = os.getcwd()
        self._materialDir = os.getcwd()
        self._animationDir = os.getcwd()
        self._textureDir = os.getcwd()
        self._texSearchDirectories = []
        
    def Save(self):
        try:
            conf = ConfigParser.ConfigParser()
            
            conf.add_section('General')
            conf.set('General', 'resource_dir', self._resourceDir)
            conf.set('General', 'model_dir', self._modelDir)
            conf.set('General', 'mesh_dir', self._meshDir)
            conf.set('General', 'material_dir', self._materialDir)
            conf.set('General', 'animation_dir', self._animationDir)
            conf.set('General', 'texture_dir', self._textureDir)
            conf.set('General', 'texture_search_directories', string.join(self._texSearchDirectories, ',').encode('gbk').strip())
            
            conf.write(open('config.ini', 'w'))
            
            return True
        
        except:
            SHOW_TRACE()
            return False
        
    def Load(self):
        try:
            conf = ConfigParser.ConfigParser()
            conf.read('config.ini')
            
            self._resourceDir = conf.get('General', 'resource_dir').strip()
            self._modelDir = conf.get('General', 'model_dir').strip()
            self._meshDir = conf.get('General', 'mesh_dir').strip()
            self._materialDir = conf.get('General', 'material_dir').strip()
            self._animationDir = conf.get('General', 'animation_dir').strip()
            self._textureDir = conf.get('General', 'texture_dir').strip()
            tsString = conf.get('General', 'texture_search_directories').decode('gbk')
            if tsString:
                self._texSearchDirectories = tsString.split(',')
            
            return True
            
        except:
            return False

__builtins__['GLOBAL_SETTING'] = GlobalSetting()

def __RemoveRootPath(path):
    path = path.replace('\\', '/')
    resDir = GLOBAL_SETTING._resourceDir.replace('\\', '/')
    if not path.startswith(resDir):
        raise 'ERROR: path is invalid.'
        
    return path[len(resDir):]

__builtins__['REMOVE_ROOT_PATH'] = __RemoveRootPath


